﻿<?xml version="1.0" encoding="utf-8" ?>
<AdoNetDataAccess connection="$ArticleDBConnection$" formatter="bizobjlist of Article(author,primaryPicture)">
	<code for="postEach" on="Article">
		<![CDATA[
			try{
				string body = CurrentBizObj.get("baseBody").ToString();
				IBizObjSource src = null;		 
				if(body.Length > 0 && ".txt".Equals(body.Substring(body.Length-4)))
					CurrentBizObj.put("baseBody",System.IO.File.ReadAllText(CurrentBizObj.get("baseBody").ToString()));
				IBizObjList list = CurrentBizObj.get("translatedBodies") as IBizObjList;
				if(list.numElements > 0)
				{
					string v = list[0].get("v").ToString();
					if(v.StartsWith("_scs_") || (v.Length > 4 && ".txt".Equals(v.Substring(v.Length-4))))
						CurrentBizObj.put("translatedBodies",App.bizObjMgr.createList(SourceType.BasicSource,System.IO.File.ReadAllText(v),"ArticleLT",null));
				}
			}catch(Exception e)
			{
				string filename= string.Format(@"{0}error.txt",App.resMgr.getMacro("BaseArticleStore"));
				System.IO.File.WriteAllText(filename,string.Format("{0}:{1}",e.Message,(e.InnerException!=null) ? e.InnerException.Message : ""));
			}
		]]>
	</code>
	<Sql>
		SELECT
		a.id, a.baseTitle, a.summary AS baseSummary, a.keyword, a.dateUpdated, a.dateCreated,
		<code>
			<![CDATA[
			if(true.Equals(info.get("includeTranslations")))
				sql.Append("a.translatedTitles,a.translatedSummaries,a.translationDetails,");
			if(true.Equals(info.get("includeBody")))
			{
				sql.Append("a.body as baseBody,");
				if(true.Equals(info.get("includeTranslations")))
					sql.Append("a.translatedBodies,");
			}
			]]>
		</code>
		u.lastName AS authorlastName, u.id AS authorid, u.firstName AS authorfirstName, u.userName AS authorusername,
		'TODO: JOIN TO GET THIS' AS mediaCacheName, p.id AS primaryPictureId,
		p.name AS primaryPictureName, p.fileName AS primaryPictureFileName, p.caption AS primaryPictureCaption,
		t.objectId, t.parentId, t.parentHostId, c.keyName, c.depth, t.rootId
		FROM
		Articles AS a LEFT OUTER JOIN
		Users AS u ON u.id = a.authorId LEFT OUTER JOIN
		Pictures AS p ON p.id = a.id
		INNER JOIN ObjectTreeNodes t ON a.companyId = t.parentHostId
		INNER JOIN ObjectTreeCounts c on c.objectId = t.objectId
		INNER JOIN ArticleAssociations aa on aa.articleId = a.id
		WHERE aa.relatedItemId = @relatedId AND associationName = @associationName
	</Sql>
	<Param name="relatedItemId" type="UniqueIdentifier" />
	<Param name="associationName" type="UnicodeVarChar" />
</AdoNetDataAccess>
